<template>
  <BaseLayout>
    <!-- 顶部菜单栏 -->
    <div class="layout__header">
      <div class="layout__header-left">
        <!-- Logo -->
        <AppLogo v-if="isShowLogo" :collapse="isLogoCollapsed" />
        <!-- 菜单 -->
        <!-- <BasicMenu :data="routes" menu-mode="horizontal" base-path="" /> -->
        <div class="topMenu_box">
          <div v-for="item in topMenuList" class="menu_box">
            <AppLink
              v-if="item.meta"
              :to="{
                path: resolvePath(item.path),
                query: item.meta.params,
              }"
            >
              <div style="margin: 10px 10px">
                <div
                  class="menu_text"
                  :class="{ menu_text_select: selectedValue == item.name }"
                  @click="menuSelect(item)"
                >
                  {{ item.meta?.title }}
                </div>
              </div>
            </AppLink>
          </div>
        </div>
      </div>
      <!-- 操作按钮 -->
      <div class="layout__header-right">
        <NavbarActions />
      </div>
    </div>

    <!-- 主内容区 -->
    <div :class="{ hasTagsView: isShowTagsView }" class="layout__main">
      <div class="content_box">
        <div v-if="leftRoutes.length > 0" class="left_menu">
          <el-scrollbar>
            <BasicMenu class="menu_left" :data="leftRoutes" base-path="" />
          </el-scrollbar>
        </div>
        <div class="app_box">
          <AppMain />
        </div>
        <!-- <TagsView v-if="isShowTagsView" /> -->
      </div>
    </div>
  </BaseLayout>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { useWindowSize } from "@vueuse/core";
import { useLayout } from "../composables/useLayout";
import { useLayoutMenu } from "../composables/useLayoutMenu";
import BaseLayout from "./BaseLayout.vue";
import AppLogo from "../components/AppLogo/index.vue";
import BasicMenu from "../components/Menu/BasicMenu.vue";
import NavbarActions from "../components/NavBar/components/NavbarActions.vue";
import TagsView from "../components/TagsView/index.vue";
import AppMain from "../components/AppMain/index.vue";
import { isExternal } from "@/utils";
import path from "path-browserify";

// 布局相关参数
const { isShowTagsView, isShowLogo } = useLayout();

// 菜单相关
const { routes } = useLayoutMenu();
const topMenuList = ref([]);
const leftRoutes = ref([]);
getTopMenuList(routes);
function getTopMenuList(routes) {
  console.log(routes);
  routes.value.forEach((element) => {
    if (!element.meta?.hidden) {
      topMenuList.value.push(element);
    }
  });
}
const selectedValue = ref("首页");
const selectList = ref({});
console.log('localStorage.getItem("loginInfo")', localStorage.getItem("loginInfo"));
if (localStorage.getItem("loginInfo") != "{}" && localStorage.getItem("loginInfo") != null) {
  selectList.value = JSON.parse(localStorage.getItem("loginInfo"));
  selectedValue.value = selectList.value.name;
  leftRoutes.value = selectList.value.children;
  console.log("获取到的数据", selectList.value, selectedValue.value);
}
// if (localStorage.getItem("loginInfo")) {
//   selectedValue.value = localStorage.getItem("loginInfo");
// }
function menuSelect(item) {
  console.log("菜单相关", item.children);
  localStorage.setItem("loginInfo", JSON.stringify(item));
  selectedValue.value = item.name;
  leftRoutes.value = item.children;
}
/**
 * 获取完整路径，适配外部链接
 *
 * @param routePath 路由路径
 * @returns 绝对路径
 */
function resolvePath(routePath: string) {
  if (isExternal(routePath)) return routePath;
  // if (isExternal(props.basePath)) return props.basePath;
  // 拼接父路径和当前路径
  return path.resolve(routePath);
}
// 响应式窗口尺寸
const { width } = useWindowSize();

// 只有在小屏设备（移动设备）时才折叠Logo（只显示图标，隐藏文字）
const isLogoCollapsed = computed(() => width.value < 768);

// onMounted(() => {
// });
</script>

<style lang="scss" scoped>
.layout {
  &__header {
    position: sticky;
    top: 0;
    z-index: 999;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    height: $navbar-height;
    // 使用 #3060ff 为主的渐变背景
    background: linear-gradient(135deg, #3060ff 0%, #1e40af 100%);
    color: #fff;
    // 添加阴影和毛玻璃效果
    box-shadow: 0 4px 20px rgba(48, 96, 255, 0.2);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);

    &-left {
      display: flex;
      flex: 1;
      align-items: center;
      min-width: 0;
      height: 100%;

      :deep(.logo) {
        flex-shrink: 0;
        height: $navbar-height;
        padding: 0 20px;
        background: rgba(255, 255, 255, 0.1);
        border-radius: 0 15px 15px 0;
        margin-right: 20px;
        transition: all 0.3s ease;

        &:hover {
          background: rgba(255, 255, 255, 0.15);
          transform: translateY(-1px);
        }
      }
    }

    &-right {
      display: flex;
      flex-shrink: 0;
      align-items: center;
      height: 100%;
      padding: 0 20px;
    }

    // 菜单样式
    :deep(.el-menu--horizontal) {
      flex: 1;
      min-width: 0;
      height: $navbar-height;
      overflow: hidden;
      line-height: $navbar-height;
      background-color: transparent;
      border: none;

      .el-menu-item {
        background-color: rgba(255, 255, 255, 0.2);
        height: 50px;
        line-height: 50px;
        border-radius: 20px;
        color: rgba(255, 255, 255, 0.9);
        font-weight: 500;
        transition: all 0.3s ease;

        &:hover {
          background-color: rgba(255, 255, 255, 0.15);
          color: #fff;
        }

        &.is-active {
          background-color: rgba(255, 255, 255, 0.2);
          color: #fff; // 选中时颜色为白色
          font-weight: bold; // 选中时加粗
          border-bottom: 3px solid #fff;
        }
      }

      .el-sub-menu {
        .el-sub-menu__title {
          height: $navbar-height;
          line-height: $navbar-height;
          color: rgba(255, 255, 255, 0.9);
          font-weight: 500;
          transition: all 0.3s ease;

          &:hover {
            color: #fff;
          }
        }

        // 父菜单激活状态 - 水平布局专用
        &.has-active-child {
          .el-sub-menu__title {
            color: #fff !important; // 选中时颜色为白色
            font-weight: bold !important; // 选中时加粗
            border-bottom: 2px solid #fff !important;

            .menu-icon {
              color: #fff !important;
            }
          }
        }
      }

      // 修复子菜单弹出位置
      .el-menu--popup {
        min-width: 160px;
      }
    }
  }

  &__main {
    height: calc(100vh - $navbar-height);
    overflow-y: auto;
  }
}

.topMenu_box {
  display: flex;
  height: $navbar-height;
  align-items: center;
  font-size: 14px;
  padding: 0 20px;
}

.menu_text {
  padding: 12px 20px;
  height: auto;
  line-height: 1.5;
  cursor: pointer;
  border-radius: 12px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.9);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s;
  }

  &:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(48, 96, 255, 0.3);

    &::before {
      left: 100%;
    }
  }
}

.menu_text_select {
  color: #fff; // 选中时颜色为白色
  font-weight: bold; // 选中时加粗
  background: rgba(255, 255, 255, 0.2);
  box-shadow: 0 4px 16px rgba(48, 96, 255, 0.2);
  border-bottom: 3px solid #fff;
}

.el-scrollbar {
  height: calc(100vh - 100px);
}

.content_box {
  display: flex;
  background-color: #f5f8fd;
}

.left_menu {
  // width: 300px;
  padding-top: 15px;
  padding-left: 15px;
  // margin-top:;
}

.menu_left {
  // margin-top: 10px/;
  width: $sidebar-width;
  border-radius: 15px;
  height: calc(100vh - 115px);
}

.app_box {
  // flex: 1;
  width: 100%;
}

// 当存在TagsView时的样式调整
.hasTagsView {
  :deep(.app-main) {
    height: calc(100vh - $navbar-height - $tags-view-height) !important;
  }
}
</style>
